package com.loan.psychicloan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.ItemClickSupport;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.Util.RecycleViewDivider;
import com.loan.psychicloan.activity.LoanInfoActivity;
import com.loan.psychicloan.adapter.LoanAdapter;
import com.loan.psychicloan.bean.LoanListBean;
import com.loan.psychicloan.bean.SpinnerItem;
import com.loan.psychicloan.bean.SpinnersearchItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by watilion on 2017/12/18 0018.
 */

public class FragmentLoanPage extends Fragment  {
    TextView textView;
    Toolbar mtoolBar;
    private long loanid;
    private Spinner spinner_amount;
    private Spinner spinner_search;
    private Spinner spinner_time;
    private RecyclerView mRecyclerView;
    private List<SpinnerItem> spinnerAmountList = new ArrayList <>();
    private List<SpinnersearchItem> spinnerSearchList = new ArrayList <>();
    private List<SpinnerItem> spinnerTimeList = new ArrayList <>();
    private List<LoanListBean.DataBean> mLoanList = new ArrayList <>();
    private String mMinamount = "0";
    private String mMaxamount = "0";
    private String mSearch = "0";
    private String mMintime = "0";
    private String mMaxtime = "0";

    public static FragmentLoanPage newInstance(){
        FragmentLoanPage fragmentLoanPage = new FragmentLoanPage();
        Bundle bundle=new Bundle();
        bundle.putString("text", "贷款");
        fragmentLoanPage.setArguments(bundle);
        return fragmentLoanPage;
    }
    @Nullable @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_loanpage,container,false);
        textView= view.findViewById(R.id.title_toolbar_titletext);
        //Log.d("xyz", "onCreateView: "+getArguments().getString("text"));
        textView.setText(getArguments().getString("text"));
        mtoolBar = view.findViewById(R.id.loanpage_toolbar);
        TextView tv_notice = view.findViewById(R.id.tv_notice_text);
        spinner_amount = view.findViewById(R.id.spinner_amount);
        spinner_search = view.findViewById(R.id.spinner_search);
        spinner_time = view.findViewById(R.id.spinner_time);
        mRecyclerView = view.findViewById(R.id.recyclerview_search);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL));

        //设置item的点击事件监听
        ItemClickSupport mItemClickSupport = ItemClickSupport.addTo(mRecyclerView);
        mItemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, View itemView, int position) {
                FragmentLoanPage.this.loanid = mLoanList.get(position).loanid;
                Bundle bundle = new Bundle();
                bundle.putLong("loanid", FragmentLoanPage.this.loanid);
                Intent mIntent = new Intent(getActivity(), LoanInfoActivity.class);
                //mIntent.putExtra("loanid",FragmentHome.this.loanid);
                mIntent.putExtras(bundle);

                startActivity(mIntent);
            }
        });

        tv_notice.setText(getString(R.string.search_notice));

        initSpinner();
        //startTask();

        spinner_time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView <?> adapterView, View view, int i, long l) {
                mMintime = ((SpinnerItem) adapterView.getSelectedItem()).getMin();
                mMaxtime = ((SpinnerItem) adapterView.getSelectedItem()).getMax();
                startTask();
                //loanAdapter.notifyDataSetChanged();
               // initAdapter();
            }

            @Override
            public void onNothingSelected(AdapterView <?> adapterView) {

            }
        });

        spinner_search.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView <?> adapterView, View view, int i, long l) {
                mSearch = ((SpinnersearchItem) adapterView.getSelectedItem()).getID();
                startTask();
                //loanAdapter.notifyDataSetChanged();
                //initAdapter();
            }

            @Override
            public void onNothingSelected(AdapterView <?> adapterView) {

            }
        });

        spinner_amount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView <?> adapterView, View view, int i, long l) {
                mMinamount = ((SpinnerItem) adapterView.getSelectedItem()).getMin();
                mMaxamount = ((SpinnerItem) adapterView.getSelectedItem()).getMax();
                startTask();
                //loanAdapter.notifyDataSetChanged();
                //initAdapter();
            }

            @Override
            public void onNothingSelected(AdapterView <?> adapterView) {

            }
        });

        return view;
    }

    private void initAdapter(List<LoanListBean.DataBean> mLoanList) {
        //创建自定义适配器对象
        LoanAdapter loanAdapter = new LoanAdapter(getContext(), mLoanList);

        //设置recyclerview适配器
        mRecyclerView.setAdapter(loanAdapter);

        //刷新适配器
        loanAdapter.notifyDataSetChanged();
    }

    private void startTask() {
        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        String URL = "https://www.psychicloan.com:8443/loan-api/loanlist/getsearchlist?";
        String minamount = "&minamount=";
        String maxtime = "&maxtime=";
        String mintime = "mintime=";
        String search = "&search=";
        String maxamount = "&maxamount=";
        String mURL = URL + mintime + mMintime + maxtime + mMaxtime + search + mSearch + minamount
                + mMinamount + maxamount + mMaxamount;
        //Log.d("mURL", "startTask: " +mURL);
        OkHttpUtils.getInstance().getBeanOfOk(getActivity(), mURL, LoanListBean.class,
                new OkHttpUtils.CallBack <LoanListBean>() {
                    @Override
                    public void getData(LoanListBean loanListBean) {
                        if (loanListBean != null) {
                            //如果不为空就用mLoanList接收

                            mLoanList.clear();
                            mLoanList.addAll(loanListBean.data);
                            //Log.d("mLoanList", "getData: " +mLoanList.toString());

                            initAdapter(mLoanList);
                        }
                    }
                });
    }

    private void initSpinner() {
        spinnerAmountList.add(new SpinnerItem("0","0","不限金额"));
        spinnerAmountList.add(new SpinnerItem("0","1000","0-1000"));
        spinnerAmountList.add(new SpinnerItem("1000","5000","1000-5000"));
        spinnerAmountList.add(new SpinnerItem("5000","10000","5000-10000"));
        spinnerAmountList.add(new SpinnerItem("10000","0","10000以上"));
        ArrayAdapter<SpinnerItem> amountAdapter = new ArrayAdapter <>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, spinnerAmountList);
        spinner_amount.setAdapter(amountAdapter);

        spinnerSearchList.add(new SpinnersearchItem("0","不限资质"));
        spinnerSearchList.add(new SpinnersearchItem("4","手机号"));
        spinnerSearchList.add(new SpinnersearchItem("3","身份证"));
        spinnerSearchList.add(new SpinnersearchItem("2","银行卡"));
        spinnerSearchList.add(new SpinnersearchItem("1","公积金"));
        ArrayAdapter<SpinnersearchItem> searchAdapter = new ArrayAdapter <>
                (getContext(), android.R.layout.simple_spinner_dropdown_item, spinnerSearchList);
        spinner_search.setAdapter(searchAdapter);

        spinnerTimeList.add(new SpinnerItem("0","0","不限期数"));
        spinnerTimeList.add(new SpinnerItem("0","30","1天-30天"));
        spinnerTimeList.add(new SpinnerItem("30","183","1月-6月"));
        spinnerTimeList.add(new SpinnerItem("183","365","6月-12月"));
        spinnerTimeList.add(new SpinnerItem("365","0","12月以上"));
        ArrayAdapter<SpinnerItem> timeAdapter = new ArrayAdapter <>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, spinnerTimeList);
        spinner_time.setAdapter(timeAdapter);

    }

}

